package com.sunfeng.linkedlist;

import java.util.Stack;

/**
 * 尾插法链表的实现
 */
public class DoubleLinkedListDemo {

    public static void main(String[] args) {

        DoubleLinkedList linkedList = new DoubleLinkedList();
        HeroNode2 heroNode1 = new HeroNode2(1, "松江", "及时雨");
        HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 heroNode3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 heroNode4 = new HeroNode2(4, "林冲", "豹子头");
        linkedList.add(heroNode1);
        linkedList.add(heroNode2);
        linkedList.add(heroNode3);
        linkedList.add(heroNode4);
        linkedList.list();
        linkedList.edit(new HeroNode2(3, "武松", "打虎"));

        linkedList.list();

    }


}

/**
 * 定义链表
 */
class DoubleLinkedList {
    /**
     * 先初始化头结点
     */
    private HeroNode2 head = new HeroNode2(0, "", "");

    /**
     * //思路，不考虑编号顺序时
     * //1、找到当前节点的最后节点
     * //2、最后的节点的next指向新节点   尾插法
     *
     * @param node
     */
    public void add(HeroNode2 node) {
        HeroNode2 temp = head;
        while (temp.next != null) {

            temp = temp.next;
        }
        temp.next = node;
        // 增加的
        node.pre = temp;
    }


    // 完成修改节点  根据节点no进行修改
    public void edit(HeroNode2 node) {
        HeroNode2 temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                System.out.println("此链表为空不能修改");
            }
            // 满足条件进行修改
            if (temp.no == node.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = node.name;
            temp.nickName = node.nickName;
        } else {
            System.out.println("没有找到节点");
        }

    }

    /**
     * 单链表删除
     *
     * @param no
     */
    public void delete(int no) {

        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {

            if (temp == null) {
                System.out.println("此链表为空不能删除");
            }
            // 满足条件进行修改
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            //被删除的节点不会有其他的指向  改变节点指向
            temp.pre.next = temp.next;
            if (temp.next != null) {
                temp.next.pre = temp.pre;
            }
        } else {
            System.out.println("没有找到节点");
        }

    }

    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        // 头结点不能动
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            temp = temp.next;
        }
    }

    public HeroNode2 getHead() {
        return head;
    }


}


//
class HeroNode2 {
    public int no;
    public String name;
    public String nickName;
    public HeroNode2 next;
    public HeroNode2 pre;
    //构造器

    public HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
